package com.lg.dborder2.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lg.dborder2.model.Country;
import com.lg.dborder2.model.SysRole;
import com.lg.dborder2.model.SysUser;
import com.lg.dborder2.service.CountryService;
import com.lg.dborder2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping
public class XiaoluoController {
    @Autowired
    private CountryService countryService;

    @Autowired
    private UserService userService;

    @RequestMapping("/say")
    public String say(){
        System.out.print("says");
        return "says";
    }

    @PostMapping(value = "country")
    public List<Country> getCountry(){
        List<Country> countries = countryService.findAll();
        return countries;
    }

    @PostMapping(value = "user")
    public List<SysUser> getUser(){
        List<SysUser> sysUsers = userService.findAll();
        return sysUsers;
    }

    @PostMapping(value = "oneuser")
    public SysUser getOne(){
        SysUser sysUser = userService.selectById((long) 1);
        return sysUser;
    }

    @PostMapping(value = "roles")
    public List<SysRole> getRolesByUserId (){
        List<SysRole> sysRoles = userService.selectRoleByUserId((long)1);
        return sysRoles;
    }


    @PostMapping(value = "insertUser")
    public Integer insert(@RequestBody String sysUser){
        Date date = new Date();
//        sysUser.setCreateTime(date);
        SysUser sysUser1 = (SysUser) JSON.parseObject(sysUser,SysUser.class);
        System.out.print(sysUser);
        Integer i = userService.insert(sysUser1);
        return i;
    }
}
